<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/7
 * Time: 11:42 AM
 */

namespace App\BO\OrderStatus\State;

use App\BO\OrderStatus\Events\OrderPayExpiredEvent;
use App\BO\OrderStatus\Events\OrderPaySuccessEvent;
use App\BO\OrderStatus\Events\OrderUserCancelEvent;
use App\BO\OrderStatus\Meta\UserCancelInfo;
use App\BO\OrderStatus\OrderStateContext;
use App\BO\PaymentState\Pay\PayInfo;

class PendingState extends AOrderState
{
    protected $desc = '待付款';

    /**
     * 付款操作
     * @param PayInfo $payInfo
     */
    public function pay(PayInfo $payInfo)
    {
        $this->stateContext->setOrderState(OrderStateContext::getInstance(DistributingState::class));
        $this->orderStateModifyStore->toDistributing();

        event(new OrderPaySuccessEvent($this->getOrder(), $payInfo));
    }

    /**
     * 过期操作
     */
    public function expire()
    {
        $this->orderStateModifyStore->toExpired();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(ExpireClosedState::class));

        event(new OrderPayExpiredEvent($this->getOrder()));
    }

    /**
     * 用户取消订单
     * @param UserCancelInfo $userCancelInfo
     */
    public function userCancel(UserCancelInfo $userCancelInfo)
    {
        $this->orderStateModifyStore->toUserCanceled();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(UserCanceledState::class));

        event(new OrderUserCancelEvent($this->getOrder(), $userCancelInfo));
    }
}
